package com.yanqu.road.dao.impl.activity.WinterLove;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.winterLove.WinterLoveUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WinterLoveUserDataDaoImpl extends TempDataObjectDao<WinterLoveUserData> {
    public static String getWinterLoveUserDataSql = "select * from `t_u_activity_winter_love_user_data` where `activityId`=? and `userId`=?";

    public WinterLoveUserData getWinterLoveUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getWinterLoveUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateWinterLoveUserDataSql = "update `t_u_activity_winter_love_user_data` set nowLevel=?,blessing=?,decoration=?,lowMapData=?,lowRewardMapData=?,lowAnimalPosition=?,highMapData=?,highRewardMapData=?,highAnimalPosition=?,beautyId=?,score=?,lowTurn=?,highTurn=?,disperseTimes=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(WinterLoveUserData winterLoveUserData) {
        winterLoveUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();

        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, winterLoveUserData.getBlessing()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getDecoration())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getLowMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getLowRewardMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getLowAnimalPosition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getHighMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getHighRewardMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getHighAnimalPosition())));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getBeautyId()));
        para.put(i++, new DbParameter(Types.BIGINT, winterLoveUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getLowTurn()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getHighTurn()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getDisperseTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, winterLoveUserData.getUserId()));
        if (getDb().executeNoneQuery(updateWinterLoveUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertWinterLoveUserDataSql = "insert into `t_u_activity_winter_love_user_data`(activityId,userId,nowLevel,blessing,decoration,lowMapData,lowRewardMapData,lowAnimalPosition,highMapData,highRewardMapData,highAnimalPosition,beautyId,score,lowTurn,highTurn,disperseTimes) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(WinterLoveUserData winterLoveUserData) {
        winterLoveUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, winterLoveUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, winterLoveUserData.getBlessing()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getDecoration())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getLowMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getLowRewardMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getLowAnimalPosition())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getHighMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getHighRewardMapData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(winterLoveUserData.getHighAnimalPosition())));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getBeautyId()));
        para.put(i++, new DbParameter(Types.BIGINT, winterLoveUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getLowTurn()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getHighTurn()));
        para.put(i++, new DbParameter(Types.INTEGER, winterLoveUserData.getDisperseTimes()));
        if (getDb().executeNoneQuery(insertWinterLoveUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected WinterLoveUserData parseData(ResultSet rs) throws SQLException {
        WinterLoveUserData winterLoveUserData = new WinterLoveUserData();
        winterLoveUserData.setActivityId(rs.getInt("activityId"));
        winterLoveUserData.setUserId(rs.getLong("userId"));
        winterLoveUserData.setLevel(rs.getInt("nowLevel"));
        winterLoveUserData.setBlessing(rs.getLong("blessing"));
        winterLoveUserData.setDecoration(JSON.parseObject(rs.getString("decoration"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.winterLove.WinterLoveBagItem>>() {
        }));
        winterLoveUserData.setLowMapData(JSON.parseObject(rs.getString("lowMapData"), new TypeReference<java.util.List<java.lang.Integer>>() {
        }));
        winterLoveUserData.setLowRewardMapData(JSON.parseObject(rs.getString("lowRewardMapData"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.winterLove.WinterLoveMapItem>>() {
        }));
        winterLoveUserData.setLowAnimalPosition(JSON.parseObject(rs.getString("lowAnimalPosition"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.winterLove.WinterLoveMapAnimal>>() {
        }));
        winterLoveUserData.setHighMapData(JSON.parseObject(rs.getString("highMapData"), new TypeReference<java.util.List<java.lang.Integer>>() {
        }));
        winterLoveUserData.setHighRewardMapData(JSON.parseObject(rs.getString("highRewardMapData"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.winterLove.WinterLoveMapItem>>() {
        }));
        winterLoveUserData.setHighAnimalPosition(JSON.parseObject(rs.getString("highAnimalPosition"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.winterLove.WinterLoveMapAnimal>>() {
        }));
        winterLoveUserData.setBeautyId(rs.getInt("beautyId"));
        winterLoveUserData.setScore(rs.getLong("score"));
        winterLoveUserData.setLowTurn(rs.getInt("lowTurn"));
        winterLoveUserData.setHighTurn(rs.getInt("highTurn"));
        winterLoveUserData.setDisperseTimes(rs.getInt("disperseTimes"));
        return winterLoveUserData;
    }

    public static String getAutoRewardUserDataSql = "select * from `t_u_activity_winter_love_user_data` where `activityId`=? ";

    public Map<Long, WinterLoveUserData> getAutoRewardUserDataMap(int activityId) {
        Map<Long, WinterLoveUserData> userDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAutoRewardUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                WinterLoveUserData parse = parse(rs);
                userDataMap.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userDataMap;
    }

}
